var layer = layui.layer //弹层
    ,table = layui.table //表格
    form = layui.form,$ = layui.jquery,laydate = layui.laydate;
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});

function getAllJson(jsons) {
    let tmpitem=[]
    for (let item of jsons) {
        let tmparr={}
        tmparr["value"]=item["code"];
        tmparr["name"]=item["name"];
        if(item["users"]){
            for(let it of item["users"]){
                item["list"].push(it)
            }
        }
        if(item["list"] && item["list"].length){
            tmparr["children"]=   this.getAllJson(item["list"]);
            tmpitem.push(tmparr)
        }else{
            tmpitem.push(tmparr)
        }

    }
    return tmpitem
}

function getxmselect(el,name, data) {
    return xmSelect.render({
        el: el,
        name: name,
        tree: {
            show: true,
            showLine: true,
            showFolderIcon:true,
            indent: 20,
            expandedKeys:false
        },
        height: 'auto',
        data: data,
        on: function ({ arr, change, isAdd }) {
            return this
        }
    })
}


//select over
(function () {
    //获取当前用户的部门及人员结构树
    let treeList = getCurrentDepartmentUser();

    if(treeList && treeList.list){
        let userTree = getAllJson(treeList.list);
        getxmselect("#operationCode","operationCode", userTree);
        getxmselect("#salesmanCode","salesmanCode", userTree);
    }else {
        getxmselect("#operationCode","operationCode", [{name: user.name, value: user.code}]);
        getxmselect("#salesmanCode","salesmanCode", []);
    }

})()


let loginUserCode;
layer.ready(function () {
});
table.render({
    elem: '#mains'
    ,url:'cost/getContractListByPage'
    , where: {
        operationCode:user.code ,
    }
    ,cols: [[
        {checkbox: true, sort : true,title:'选择'}
        ,{field:'customerName', title:'客户名称',sort:true}
        ,{field: 'exportContractNo', title: '外销合同号', sort: true}
        ,{field: 'purchaseContractNo', title: '采购合同号', sort: true}
        ,{field: 'signingDateStr', title: '签约日期', sort: true}
        ,{field:'salesmanName', title:'业务员',sort:true}
        ,{field:'salesmanCode', hide:true}
        ,{field:'operatorName', title:'操作员',sort:true}
        ,{field:'operatorCode', hide:true}
        ,{field:'billCompanyName', title:'制单公司',sort:true}
        ,{field:'billCompanyCode', hide:true}
    ]]
    ,id: 'mainstableid'
    ,height: 500
    ,page: true
});

form.on('submit(submitform)', function(data){
    table.reload('mainstableid', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    return false;
});

$("#choose").on("click",function () {
    var purchaseorderdata =   table.checkStatus("mainstableid").data;
    if(purchaseorderdata.length == 0){
        layer.msg("请选择数据")
    }else{
        var oldData =  parent.table.cache["maininformationtableid"];
        $.each(purchaseorderdata, function (index, value) {
            purchaseorderdata[index]['tempId']=Number(Math.random().toString().substr(3,3) + Date.now()).toString(36);
            purchaseorderdata[index]['billDate']=value.signingDateStr;
            purchaseorderdata[index]['contractNo']=value.exportContractNo;
            purchaseorderdata[index]['stockNoStr']=value.purchaseContractNo;
            purchaseorderdata[index]['increaseCode'] = "无";
            purchaseorderdata[index]['id'] = null;

            oldData.push(purchaseorderdata[index]);
        });
        parent.table.reload('maininformationtableid', {data:oldData, limit:oldData.length}); //刷新主信息表格
        //执行接口
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },1000)
    }
})
$("#shutdown").on("click",function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})